package com.itheima.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 注：servlet使用4.0.1的包，如果是3.0需要重写三个方法
 */
@WebFilter(filterName = "CharacterEncodingFilter", urlPatterns = "/*")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //将请求对象转成子接口
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        if (httpServletRequest.getMethod().equals("POST")) {  //注：POST一定是大写
            //只要写一句话就可以了
            request.setCharacterEncoding("utf-8");
        }
        //放行
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }


}
